const nonNull = (obj)=> {
  if (obj == undefined || obj == null || obj == "") {
    return false
  }
  return true
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const formatTimeFromStamp = stamp => {
  const date = new Date(stamp)
  return formatTime(date)
}

const keepTwoDecimal = n => {
  var result = parseFloat(n);
  if (isNaN(result)) {
      alert('传递参数错误，请检查！');
      return false;
  }
  result = Math.round(n * 100) / 100;
  return result;
}

const paramsWrap = params => {
  return '?'+Object.keys(params).map(key => key+"="+params[key]).join("&")
}

const wifiConnect = params => {
  let ssid = params.ssid
  let pwd = params.password
  //检测手机型号
  wx.getSystemInfo({
    success: (res)=> {
      var system = ''
      if (res.platform == 'android') system = parseInt(res.system.substr(8))
      if (res.platform == 'ios') system = parseInt(res.system.substr(4))
      if (res.platform == 'android' && system < 6) {
        ui.showToast({
          title: '手机版本不支持'
        })
        return
      }
      if (res.platform == 'ios' && system < 11.2) {
        ui.showToast({
          title: '手机版本不支持'
        });
        return
      }
      //2.初始化 Wi-Fi 模块
      wx.startWifi({
        success: function() {
          //请求成功连接Wifi
          wx.connectWifi({
            SSID: ssid,
            password: pwd,
            success: function(res) {
              ui.showToast({
                title: 'wifi连接成功'
              })
            },
            fail: function(res) {
              ui.showToast({
                title: 'wifi连接失败'
              })
            }
          })
        },
        fail: function(res) {
          ui.showToast({
            title: '接口调用失败'
          })
        }
      })
    }
  })
}

module.exports = {
  nonNull,
  formatTime,
  formatTimeFromStamp,
  keepTwoDecimal,
  paramsWrap,
  wifiConnect,
}
